// Copyright (c) 2024 CHANGLEI. All rights reserved.

import 'dart:async';

import 'package:charts_grasp/src/circular_chart/base/circular_area.dart';
import 'package:charts_grasp/src/circular_chart/base/circular_base.dart';
import 'package:charts_grasp/src/circular_chart/base/series_base.dart';
import 'package:charts_grasp/src/circular_chart/renderer/chart_point.dart';
import 'package:charts_grasp/src/circular_chart/renderer/data_label_renderer.dart';
import 'package:charts_grasp/src/circular_chart/renderer/renderer_extension.dart';
import 'package:charts_grasp/src/common/rendering_details.dart';
import 'package:charts_grasp/src/common/state_properties.dart';
import 'package:charts_grasp/src/common/user_interaction/tooltip.dart';
import 'package:charts_grasp/src/common/user_interaction/tooltip_rendering_details.dart';
import 'package:flutter/material.dart';

class CircularStateProperties extends StateProperties {
  CircularStateProperties({required RenderingDetails renderingDetails, required this.chartState})
      : super(renderingDetails, chartState) {
    renderingDetails.didSizeChange = false;
  }

  @override
  CgCircularChart get chart => chartState.widget;

  @override
  // ignore: overridden_fields
  final CgCircularChartState chartState;

  late Offset centerLocation;

  late List<Rect> annotationRegions;

  CircularDataLabelRenderer? renderDataLabel;

  CircularSeriesRendererExtension? prevSeriesRenderer;

  List<ChartPoint<dynamic>?>? oldPoints;

  late CircularSeriesBase chartSeries;

  late CircularArea circularArea;

  late bool needToMoveFromCenter;

  late bool needExplodeAll;

  late bool isToggled;

  bool? requireDataLabelTooltip;

  late bool isRtl;

  bool legendRefresh = false;

  void redraw() {
    renderingDetails.initialRender = false;
    if (renderingDetails.isLegendToggled) {
      isToggled = true;
      prevSeriesRenderer = chartSeries.visibleSeriesRenderers[0];
      oldPoints = List<ChartPoint<dynamic>?>.filled(prevSeriesRenderer!.renderPoints!.length, null);
      for (int i = 0; i < prevSeriesRenderer!.renderPoints!.length; i++) {
        oldPoints![i] = prevSeriesRenderer!.renderPoints![i];
      }
    }
    final TooltipRenderingDetails tooltipRenderingDetails =
        TooltipHelper.getRenderingDetails(renderingDetails.tooltipBehaviorRenderer);
    if (tooltipRenderingDetails.chartTooltipState != null) {
      tooltipRenderingDetails.show = false;
    }
    if (chartState.mounted) {
      // ignore: invalid_use_of_protected_member
      chartState.setState(() {});
    }
  }

  bool get animationCompleted {
    return renderingDetails.animationController.status != AnimationStatus.forward;
  }

  Timer? tooltipTimer;

  bool isTooltipOrientationChanged = false;

  bool isTooltipHidden = false;
}
